package entity;

/**
 * 顺序二叉树
 *
 * @Author: wanqian
 * @Date: 2021/2/5 10:24
 */
public class ArrBinaryNode {

    private int[] arr;

    public ArrBinaryNode(int[] arr) {
        this.arr = arr;
    }

    public void preOrder() {
        if (arr == null && arr.length == 0) {
            System.out.println("二叉树为空!");
            return;
        }

        preOrder(0);

    }

    /**
     * 前序遍历
     *
     * @param index
     */
    private void preOrder(int index) {
        System.out.println(arr[index]);

        if (2 * index + 1 < arr.length) {
            preOrder(2 * index + 1);
        }

        if (2 * index + 2 < arr.length) {
            preOrder(2 * index + 2);
        }
    }

    public void infixOrder() {
        if (arr == null && arr.length == 0) {
            System.out.println("二叉树为空!");
            return;
        }
        infixOrder(0);
    }

    /**
     * 中序遍历
     *
     * @param index
     */
    private void infixOrder(int index) {

        if (2 * index + 1 < arr.length) {
            infixOrder(2 * index + 1);
        }

        System.out.println(arr[index]);

        if (2 * index + 2 < arr.length) {
            infixOrder(2 * index + 2);
        }
    }

}
